﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.DisplayManagement.Descriptors;
using Orchard.Themes.Services;
using Orchard.FileSystems.VirtualPath;
using Orchard;
using Orchard.ContentManagement;
using Orchard.DisplayManagement.Implementation;

using LuDC.CKEditor.Models;
using LuDC.CKEditor.Services;

namespace CKEditor
{
	public class Shapes : IShapeTableProvider
	{
		private readonly Lazy<IEnumerable<ICKEditorConfigProvider>> _ckEditorConfigProviders;
		public IOrchardServices Services { get; set; }

		public Shapes(
			IOrchardServices orchardServices,
			Lazy<IEnumerable<ICKEditorConfigProvider>> configProviders
			)
		{
			_ckEditorConfigProviders = configProviders;

			Services = orchardServices;

		}

		public void Discover(ShapeTableBuilder builder)
		{
			builder.Describe("CKEditor_Config")
				.OnDisplaying(CKEditor_Config_Displaying);
		}

		public void CKEditor_Config_Displaying(ShapeDisplayingContext displaying)
		{
			// Build other key/value pairs
			var context = new CKEditorConfigContext();
			foreach (var provider in _ckEditorConfigProviders.Value)
			{
				provider.PopulateConfiguration(context);
			}
			displaying.Shape.ConfigPairs = context.Config.Values;
		}
	}
}